package com.teach.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.teach.Excption.CodeException;
import com.teach.dao.UserDao;
import com.teach.entity.user;
import com.teach.service.UserService;

import cn.hutool.core.util.StrUtil;

public class UserServiceImpl implements UserService{
	private UserDao userDAO = new UserDao();
	
	@Override
	public List<user> list() {
		try {
			return userDAO.selectAll();
		} catch (SQLException e) {
			throw new RuntimeException("获取用户列表失败",e);
		}
		
	}
	
	@Override
	public user login(String name,String password) {
		user u=new user();
		try {
			u = userDAO.selectByName(name);
		} catch (SQLException e) {
			throw new CodeException(1,"用户不存在",e);
		}
		if (!u.getPassword().equals(password)) {
			throw new CodeException(2,"密码错误");
		}

		return u;
		
	}
	@Override
	public void add(user u) {
		if(StrUtil.hasBlank(u.getName())){
			throw new RuntimeException("用户名不能为空");
		}
		
		//if(user.getUserPassword()==null || user.getUserPassword().equals("")){
		if(StrUtil.hasEmpty(u.getName())){
			throw new RuntimeException("密码不能为空");
		}
		
		
		try {
			UserDao.add(u);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException("删除用户失败", e);
		}
	}

	

	@Override
	public void deleteById(int id) {
		try{
			 userDAO.deleteById(id);
		}catch(Exception ex){
			throw new RuntimeException("删除用户失败", ex);
		}
	}

	@Override
	public void updateById(user u) {
		
		try{
			 userDAO.updateById(u);
		}catch(Exception ex){
			throw new RuntimeException("删除用户失败", ex);
		}
	}

	@Override
	public user select(int id) {
			user  u=new user();
			try {
				u=userDAO.selectById(id);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw new RuntimeException("查找用户失败", e);
			}
			return u;

	}
	

	@Override
	public int count() {
		int counts = 0;
		try {
			counts=userDAO.countAll();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			return counts;
		}
		
	}
	
/**
 * current:当前页码
 * rowMax：每页最大行数
 */
	@Override
	public List<user> page(int current, int rowMax) {
		try {
			return	userDAO.selectIndex((current-1)*rowMax, rowMax);
		} catch (SQLException e) {
			
			throw new RuntimeException("获取用户列表失败",e);
		}
	}
	
	
	@Override
public List<user> SelectByString(String s) {
try {
	return userDAO.SelectByString(s);
} catch (SQLException e) {
	throw new RuntimeException("获取用户列表失败",e);
}
	
}

	


	

}
